from PySide6.QtWidgets import *
from PySide6.QtCore import Qt,QSize,Signal,Slot,QTimer,QRect


class LabelDisp(QWidget):

    sig_textUpdata  = Signal(object)

    def __init__(self,tabWidget,config):
        super(LabelDisp, self).__init__()
        self.tabWidget = tabWidget
        self.config = config
        self.lbl_list = []
        self.ldt_list = []

        self.lab_num = 160      # 从配置文件中获取监控的变量的个数

        self.topFiller = QWidget()
        self.topFiller.resize(self.tabWidget.width()-20, 2000)

        self.scroll = QScrollArea(self.tabWidget)
        self.scroll.setWidget(self.topFiller)
        self.scroll.setMinimumSize(self.tabWidget.width()-20,self.tabWidget.height())

    # 获取串口接收并解析后的数据显示出来
    def slotDispText(self,frameEleList):
        print("LabelDisp frameEleList:",frameEleList)
        for i in range(len(frameEleList)): 
            self.ldt_list[i].setText(str(frameEleList[i]) + ":" + str(i))

    # 创建显示标签
    def createDispLable(self):
        lbl_new = QLabel(self.topFiller)
        self.lbl_list.append(lbl_new)
        self.lbl_list[-1].setToolTip("当前时间")
        self.lbl_list[-1].setObjectName("lbl_new:currentTime")
        self.lbl_list[-1].setText("当前时间")
        self.lbl_list[-1].setFixedWidth(100)
        ldt_new = QLineEdit(self.topFiller)
        self.ldt_list.append(ldt_new)
        self.ldt_list[-1].setObjectName("ldt_new:currentTime")
        self.ldt_list[-1].setReadOnly(True)
        self.ldt_list[-1].setText("0")
        self.ldt_list[-1].setFixedWidth(100)
        type = self.config["frameType"]["1"]
        for short_i in list(self.config[type]["element_dict"].keys()):
            for var_j in list(self.config[type]["element_dict"][short_i].keys()):     
                lbl_new = QLabel(self.topFiller)
                self.lbl_list.append(lbl_new)
                self.lbl_list[-1].setToolTip(self.config[type]["element_dict"][short_i][var_j]["name"])
                self.lbl_list[-1].setObjectName("lbl_new" + short_i + var_j)
                self.lbl_list[-1].setText(self.config[type]["element_dict"][short_i][var_j]["name"])
                self.lbl_list[-1].setFixedWidth(100)
                ldt_new = QLineEdit(self.topFiller)
                self.ldt_list.append(ldt_new)
                self.ldt_list[-1].setObjectName("ldt_new" + short_i + var_j)
                self.ldt_list[-1].setReadOnly(True)
                self.ldt_list[-1].setText(short_i + var_j)
                self.ldt_list[-1].setFixedWidth(100)

    def lableDispLayout(self):
        self.topFiller.resize(self.tabWidget.width()-20, 2000)
        self.scroll.setMinimumSize(self.tabWidget.width(),self.tabWidget.height())
        i = 0
        j = 0
        for num in range(0,len(self.lbl_list)):
            self.lbl_list[num].setGeometry(QRect(10+i*(self.lbl_list[num].width()+10), 10+j*50, self.lbl_list[num].width(), 20))
            self.ldt_list[num].setGeometry(QRect(10+i*(self.ldt_list[num].width()+10), 30+j*50, self.ldt_list[num].width(), 20))
            i += 1
            if 10+i*(self.ldt_list[num].width()+10) + 10 > self.topFiller.width():
                i = 0
                j += 1








